package com.sg.business.model.dataset.projectgroup;

import com.mobnut.db.model.ModelService;
import com.mobnut.db.model.PrimaryObject;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.sg.business.model.IModelConstants;
import com.sg.business.model.Project;
import com.sg.business.model.ProjectBudget;
import com.sg.business.model.ProjectGroup;
import com.sg.widgets.commons.dataset.MasterDetailDataSetFactory;

public class BudgetOfProjectGroup extends MasterDetailDataSetFactory {

	public BudgetOfProjectGroup() {
		super(IModelConstants.DB, IModelConstants.C_PROJECT);
	}

	@Override
	protected String getDetailCollectionKey() {
		return Project.F__ID;
	}

	@Override
	protected Object getMasterValue() {
		if (master instanceof ProjectGroup) {
			return new BasicDBObject().append("$in",
					((ProjectGroup) master).getProjectIds());
		}
		return super.getMasterValue();
	}

	@Override
	protected PrimaryObject getDataItem(DBObject dbo) {
		Project project = ModelService.createModelObject(dbo, Project.class);
		if (project != null) {
			ProjectBudget projectBudget = project.getBudget();
			if (projectBudget != null) {
				project = projectBudget.makeHasBudgetTableProject(project);
			}
			return project;
		}

		return super.getDataItem(dbo);
	}

}
